package com.material.vtool.controller;

import com.google.common.collect.Maps;
import com.material.vtool.entity.BTeamInfoEntity;
import com.material.vtool.entity.BTeamRelationEntity;
import com.material.vtool.entity.combinedEntity.CombinedTeamEntity;
import com.material.vtool.entity.reqEntity.TeamBean;
import com.material.vtool.entity.reqEntity.UpdateTeamItemBean;
import com.material.vtool.entity.reqEntity.UpdateTeamNameBean;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.service.TeamInfoService;
import com.material.vtool.service.TeamRelationService;
import com.material.vtool.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 团队
 */
@RestController
@RequestMapping("/team")
public class TeamInfoController {
    @Autowired
    private TeamInfoService teamInfoService;

    /**
     * 创建团队
     * @param miniCode 小程序标识
     * @param userId 用户ID
     * @param teamBean 团队信息
     * @return
     */
    @PostMapping(value = "/createTeam", produces = "application/json;charset=UTF-8")
    public RespData createTeam(@RequestHeader("userId") String userId, @RequestHeader("miniCode") String miniCode, @RequestBody TeamBean teamBean) {
        // 数据校验
        if (StringUtils.isBlank(miniCode) || StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)
                || teamBean == null || StringUtils.isBlank(teamBean.getTeamName())) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }

        Integer currentUserId = Integer.valueOf(userId);

        BTeamInfoEntity bTeamInfoEntity = new BTeamInfoEntity();
        bTeamInfoEntity.setTeamName(teamBean.getTeamName());
        bTeamInfoEntity.setTeamDesc(teamBean.getTeamDesc());
        bTeamInfoEntity.setTeamImageUrl(teamBean.getTeamImageUrl());
        bTeamInfoEntity.setUserId(currentUserId);
        bTeamInfoEntity.setMiniCode(miniCode);
        bTeamInfoEntity.setCreateTime(DateUtil.getCurDate());
        teamInfoService.saveTeamInfo(bTeamInfoEntity);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 修改团队名称
     * @param updateTeamNameBean 团队修改信息实体
     * @return
     */
    @PostMapping(value = "/updateTeamName", produces = "application/json;charset=UTF-8")
    public RespData updateTeamName(@RequestBody UpdateTeamNameBean updateTeamNameBean) {
        if (updateTeamNameBean == null || updateTeamNameBean.getTeamId() == null || StringUtils.isBlank(updateTeamNameBean.getTeamName())) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }
        BTeamInfoEntity bTeamInfoEntity = new BTeamInfoEntity();
        bTeamInfoEntity.setId(updateTeamNameBean.getTeamId());
        bTeamInfoEntity.setTeamName(updateTeamNameBean.getTeamName());
        bTeamInfoEntity.setModifyTime(DateUtil.getCurDate());
        teamInfoService.updateTeam(bTeamInfoEntity);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 修改团队项目
     * @param updateTeamItemBean 团队修改项目实体
     * @return
     */
    @PostMapping(value = "/updateTeamItem", produces = "application/json;charset=UTF-8")
    public RespData updateTeamItem(@RequestBody UpdateTeamItemBean updateTeamItemBean) {
        if (updateTeamItemBean == null || updateTeamItemBean.getTeamId() == null || StringUtils.isBlank(updateTeamItemBean.getTeamDesc())) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }
        BTeamInfoEntity bTeamInfoEntity = new BTeamInfoEntity();
        bTeamInfoEntity.setId(updateTeamItemBean.getTeamId());
        bTeamInfoEntity.setTeamDesc(updateTeamItemBean.getTeamDesc());
        bTeamInfoEntity.setModifyTime(DateUtil.getCurDate());
        teamInfoService.updateTeam(bTeamInfoEntity);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 按团队ID或团队名称查找团队
     * @param miniCode 小程序标识
     * @param key 搜索关键字
     * @return
     */
    @GetMapping(value = "/getTeamByIdOrName", produces = "application/json;charset=UTF-8")
    public RespData getTeamByIdOrName(@RequestHeader("miniCode") String miniCode, @RequestParam("key") String key) {
        if (StringUtils.isBlank(miniCode) || StringUtils.isBlank(key)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }

        List<CombinedTeamEntity> combinedTeamEntityList = teamInfoService.getTeamByIdOrName(miniCode, key);
        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("teamList", combinedTeamEntityList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 根据团队ID获取团队信息
     * @param teamId 团队ID
     * @return
     */
    @GetMapping(value = "/getTeamInfoByTeamId", produces = "application/json;charset=UTF-8")
    public RespData getTeamInfoByTeamId(@RequestParam Integer teamId) {
        if (teamId == null || teamId.intValue() <= 0) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }

        BTeamInfoEntity bTeamInfoEntity = teamInfoService.getTeamInfoByTeamId(teamId);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("teamInfo", bTeamInfoEntity);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }
}
